<?php
namespace app\admin\controller;

use app\common\controller\Admin ; 

class Role extends Admin
{
    


    //控制器初始化
    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('Role');
        //$this->Dept = model('Dept');
    }
    /**
     * 组织管理 
     */
    public function index()
    {
        $map = array();


        $this->setMeta('系统管理|岗位管理');
        return view();
    }
    
    public function lists(){
        $dept_id = input('dept_id/d');
        $ids = $this->model->Dept->tree->getids( $dept_id ); 

        $map = array();
        if($ids){ $map['dept_id'] = [ 'in' , $ids ] ;}
        $list = $this->model->where($map)
        ->paginate(20,false,['query' => request()->param()]);

        $this->assign('dept_id',$dept_id);
        $this->assign('list',$list);
        $this->setMeta('系统管理|岗位管理');
        return view();
    }

    /**
     * 添加 or  修改
     * @return [type]           [description]
     */
    public function edit(){

        if(IS_AJAX){
            $data   = $this->request->post();
            if ($data) {

                $result = $this->model->isUpdate(!!$data['id'])->validate(true)->save($data);
                if (false === $result) {
                    return $this->error($this->model->getError() );
                } else {
                    return $this->success('更新成功');
                }
            } else {
                return $this->error( $this->model->getError() );
            }  
        }else{

            $id = input('id/d' , 0 );
            $dept_id = input('dept_id/d' , -1 );
            $dopost = input('dopost/s' , '' );


            $info = $this->model-> get($id); //读取数据 

            if(!$info){   $info['dept_id'] = $dept_id ; };

            if('copy'==$dopost){
                $info['id'] = 0 ; 
                $info['name'] .= ' - 【克隆】' ; 
            } //copy 


            $this->assign('info' , $info );
            $this->setMeta('系统管理|岗位管理');
            return view();
        }


        
    }


    /**
     * 删除配置  一个删除 和 多个删除
     *  
     */
    public function del() {
        $id = array_unique((array) input('id/a', 0));
        $rs = $this->model->del($id);
        if ( $rs === false ) {
            return $this->error('删除失败！' );    // $msg = '', $url = null, $data = '', $wait = 3, array $header = []
        } else {
            return $this->success('删除成功！' );   //$msg = '', $url = null, $data = '', $wait = 3, array $header = []
        }
    }




    


}
